import Vue from 'vue';
import VueRouter from 'vue-router';

// 注册组件 <router-view> <router-link>
Vue.use(VueRouter);

import Home from '../views/Home.vue'
import Shop from '../views/Shop.vue'
import Order from '../views/Order.vue'
import Cart from '../views/Cart.vue'
import NotFound from '../views/NotFound.vue'
import TopNav from '../views/TopNav.vue'
import TopBar from '../views/TopBar.vue'
import Layout from '../views/Layout.vue'

const router = new VueRouter({
  mode: 'history',
  // 路由表
  routes: [
    {
      path: '/home/:id',
      name: 'home',
      component: Home,
      beforeEnter(to, from, next) {
        console.log('home beforeEnter');
        next();
        // if (from.name === 'cart' || from.name === 'order') {
        //   next();
        // } else {
        //   next(false);
        // }
      },
    },
    {
      path: '/order',
      name: 'order',
      component: Order,
    },
    {
      path: '/cart',
      name: 'cart',
      component: Cart,
    },
    {
      path: '/shop',
      name: 'shop',
      component: Shop,
    },
  ]
});


// 捕获路由对象导航错误
router.onError((error) => {
  console.log(error);
});

router.beforeEach((to, from, next) => {
  console.log('beforeEach');
  next();
  // if (to.name === 'shop' || to.name === 'home') {
  // if (to.name === 'shop' || to.name === 'home') {
  //   next();//放行
  // } else {
  //   // next(false);//拦截
  //   // next('/shop');//拦截
  //   // next({ path: '/shop' });//拦截
  //   // next({ name: 'shop' });//拦截
  //   next(new Error('...'));//拦截
  // }
})

router.afterEach((to, from) => {
  console.log('afterEach');
})

router.beforeResolve((to, from, next) => {
  console.log('beforeResolve');
  next();
})



export default router;